package com.gs.dao.jdbc.impl;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;

import com.gs.action.datamodel.ModuelDateModel;
import com.gs.bean.Moduel;
import com.gs.dao.ModuleDAO;
import com.gs.dao.jdbc.impl.rowmapper.ModuleRowMapper;
import com.liang.log.Log;
import com.liang.util.Page;

@Controller(value = "moduleDAOImpl")
public class ModuleDAOImpl extends BaseDAOImpl implements ModuleDAO {


	public List<Moduel> findModules(Integer pid) {
		String sql = "select * from moduel_info where ";
		if (null == pid) {
			sql += "pid is null";
			return jdbcTemplate.query(sql, new Object[] {},
					new ModuleRowMapper());
		}
		sql += "pid= ?";
		return jdbcTemplate.query(sql, new Object[] { pid },
				new ModuleRowMapper());

	}

	@Override
	public void deleteModel(Integer id) throws Exception {
		String sql = "delete from moduel_info where id = ?";
		jdbcTemplate.update(sql, new Object[] { id },
				new int[] { Types.INTEGER });
	}

	@Override
	public void deleteModel(Moduel model) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Moduel findModel(Integer id) throws Exception {
		String sql = "select * from moduel_info where id= ?";
		return (Moduel) jdbcTemplate.queryForObject(sql, new Object[] { id },
				new ModuleRowMapper());
	}

	@Override
	public void saveModel(Moduel model) throws Exception {
		String sql = "insert into moduel_info(name,sn,url,pid,orderno) values(?,?,?,?,?)";
		jdbcTemplate.update(sql, new Object[] { model.getName(),
				model.getSn(), model.getUrl(), model.getPid(),
				model.getOrderno() }, new int[] { Types.VARCHAR,
				Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.INTEGER });
	}

	@Override
	public void updataModel(Moduel model) throws Exception {
		// TODO Auto-generated method stub
		
	}


	@Override
	public Page<Moduel> queryPage(ModuelDateModel searchModel, int currentPage,
			int pageSize) throws Exception {
		Page<Moduel> page  = null;
		String _queryQuery = "select * from moduel_info";
		StringBuffer _queryWhere = new StringBuffer();
		List<Moduel> modules = null;
		if(null == searchModel || null == searchModel.getModuel()){
			modules = jdbcTemplate.query(_queryQuery, new ModuleRowMapper());
		}else{
			List<Object> _args = new ArrayList<Object>(0);
			List<Integer> _argTypeObjs = new ArrayList<Integer>(0);
			Moduel moduel = searchModel.getModuel();
			if(moduel.getId() != null){
				_queryWhere.append(" id = ?");
				_args.add(moduel.getId());
				_argTypeObjs.add(Types.INTEGER);
			}
			if(moduel.getName() != null){
				_queryWhere.append(" name like ?");
				_args.add('%'+moduel.getName()+'%');
				_argTypeObjs.add(Types.VARCHAR);
			}
			if(moduel.getOrderno() != null){
				_queryWhere.append(" orderno = ?");
				_args.add(moduel.getOrderno());
				_argTypeObjs.add(Types.INTEGER);
			}
			if(moduel.getPid() != null){
				_queryWhere.append(" pid = ?");
				_args.add(moduel.getPid());
				_argTypeObjs.add(Types.INTEGER);
			}
			if(moduel.getSn() != null){
				_queryWhere.append(" sn like ?");
				_args.add('%'+moduel.getSn()+'%');
				_argTypeObjs.add(Types.VARCHAR);
			}
			if(moduel.getUrl() != null){
				_queryWhere.append(" url like ?");
				_args.add('%'+moduel.getUrl()+'%');
				_argTypeObjs.add(Types.VARCHAR);
			}
			if(!_queryWhere.toString().equals("")){
				_queryWhere.insert(0, " where");
			}
			_queryQuery += _queryWhere.toString();
			
			Log.info("sql:"+_queryQuery);
			int[] _argTypes = new int[_argTypeObjs.size()];
			for(int i = 0 ;  i < _argTypeObjs.size() ; i++){
				_argTypes[i] = _argTypeObjs.get(i);
			}
			modules = jdbcTemplate.query(_queryQuery, _args.toArray(), _argTypes, new ModuleRowMapper());
		}
		
		if(null == modules){
			modules = new ArrayList<Moduel>(0);
		}
		
		page = new Page<Moduel>(modules , currentPage , pageSize , modules.size());
		return page;
	}

	
}
